package data;

import java.util.HashMap;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DBLogin extends bbdd{
	
	 public static String Table = "login";
	 
	    private static final String KEY_ID = "id";
	    private static final String KEY_NAME = "name";
	   // private static final String KEY_SURNAME = "surname";
	    private static final String KEY_EMAIL = "email";
	    private static final String KEY_UID = "uid";
	    private static final String KEY_CREATED_AT = "created_at";
	 
	 public DBLogin(Context context) {
	  super(context);  
	 }
	 

	    public void addUser(String name, String email, String uid, String created_at) {
	        SQLiteDatabase db = this.getWritableDatabase();
	 
	        ContentValues values = new ContentValues();
	        values.put(KEY_NAME, name);
	        //values.put(KEY_SURNAME, surname);
	        values.put(KEY_EMAIL, email);
	        values.put(KEY_UID, uid); 
	        values.put(KEY_CREATED_AT, created_at); 
	 
	        
	        db.insert(Table, null, values);
	        db.close();
	    }
	 
	    /**
	     * Obtener usuario de la base de datos
	     * */
	    public HashMap<String, String> getUserDetails(){
	        HashMap<String,String> user = new HashMap<String,String>();
	        String selectQuery = "SELECT  * FROM " + Table;
	 
	        SQLiteDatabase db = this.getReadableDatabase();
	        Cursor cursor = db.rawQuery(selectQuery, null);

	        cursor.moveToFirst();
	        if(cursor.getCount() > 0){
	            user.put("name", cursor.getString(1));
	            user.put("email", cursor.getString(2));
	            user.put("uid", cursor.getString(3));
	            user.put("created_at", cursor.getString(4));
	        }
	        cursor.close();
	        db.close();

	        return user;
	    }
	 
	  
	    public int getRowCount() {
	        String countQuery = "SELECT  * FROM " + Table;
	        SQLiteDatabase db = this.getReadableDatabase();
	        Cursor cursor = db.rawQuery(countQuery, null);
	        int rowCount = cursor.getCount();
	        db.close();
	        cursor.close(); 
	       
	        return rowCount;
	    }
	    
	    public Cursor getCursorUsuario(){
	     SQLiteDatabase db = getReadableDatabase();
	  
	  return db.rawQuery("SELECT name, email, uid " +
	    " FROM "+Table 
	    
	    , null); 
	    }
	 
	  
	    public void resetTables(){
	        SQLiteDatabase db = this.getWritableDatabase();
	        db.delete(Table, null, null);
	        db.close();
	    }

}
